package likou;

import java.util.ArrayDeque;
import java.util.Deque;

/**
 * @Info:
 * @Author: 唐小尊
 * @Date: 2020/10/15 10:22
 */
public class _116 {
    public Node connect(Node root) {
        Deque<Node> dequeA = new ArrayDeque<>();
        Deque<Node> dequeB = new ArrayDeque<>();
        if (root == null) {
            return root;
        }
        dequeA.addAll(dequeB);
        System.out.println(root.val);
        dequeB.offerLast(root);
        while (dequeA.size() > 0) {
            dequeA = dequeB;
            dequeB = new ArrayDeque();
            while (dequeA.size() > 0) {
                Node now = dequeA.pollFirst();
                System.out.println(now.val);
                if (dequeA.size() > 1) {
                    now.next = dequeA.peekFirst();
                }
                if (now.left != null) {
                    dequeB.offerLast(now.left);
                }
                if (now.right != null) {
                    dequeB.offerLast(now.right);
                }
            }
        }
        return root;
    }
}

class Node {
    public int val;
    public Node left;
    public Node right;
    public Node next;

    public Node() {
    }

    public Node(int _val) {
        val = _val;
    }

    public Node(int _val, Node _left, Node _right, Node _next) {
        val = _val;
        left = _left;
        right = _right;
        next = _next;
    }
};